package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车持久层实现类
 */
@Service
public class ShoppingCartSerrviceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 新增购物车
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //查询购物车，判断当前新驾到购物车的数据，是否已经存在购物车表中
        //前端传递的菜品或者是套餐的id
        ShoppingCart shoppingCartQuery = new ShoppingCart();
        //shoppingCartDTO  ===>  shoppingCartQuery
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCartQuery);
        //当前用户id
        shoppingCartQuery.setUserId(BaseContext.getCurrentId());
        //查询购物车表，查看当前是否有符合当前条件的数据
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCartQuery);
        //判断当前集合中是否有数据
        if(CollectionUtils.isNotEmpty(shoppingCartList) && shoppingCartList.size()==1){
            //如果符合条件，就需要更新数量+1
            ShoppingCart shoppingCart = shoppingCartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.updateNumberById(shoppingCart);
        }else {
            //数据新增
            //判断当前的数据是菜品还是套餐
            //判断是否是菜品
            //查询菜品
            Dish dish = dishMapper.getById(shoppingCartQuery.getDishId());
            if (shoppingCartQuery.getDishId() != null){
                //菜品名称
                shoppingCartQuery.setName(dish.getName());
                //图片
                shoppingCartQuery.setImage(dish.getImage());
                //单价
                shoppingCartQuery.setAmount(dish.getPrice());
            }else {
                Setmeal setmeal = setmealMapper.getById(shoppingCartQuery.getSetmealId());
                //套餐名称
                shoppingCartQuery.setName(setmeal.getName());
                //图片
                shoppingCartQuery.setImage(setmeal.getImage());
                //单价
                shoppingCartQuery.setAmount(setmeal.getPrice());
            }
            //数量
            shoppingCartQuery.setNumber(1);
            //创建时间
            shoppingCartQuery.setCreateTime(LocalDateTime.now());
            //新增到数据库
            shoppingCartMapper.insert(shoppingCartQuery);

        }
    }

    /**
     * 查看购物车
     */
    @Override
    public List<ShoppingCart> showShoppingCart( ) {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List list = shoppingCartMapper.list(shoppingCart);
        return list;
    }

    /**
     * 清空购物车
     */
    @Override
    public void delete() {
        shoppingCartMapper.delete(BaseContext.getCurrentId());
        return;
    }

    /**
     * 减少购物车
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //设置查询条件，查询当前登录用户的购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());

        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if (list != null && list.size() >0){
            shoppingCart = list.get(0);
            Integer number = shoppingCart.getNumber();
            if (number == 1 ){
                shoppingCartMapper.deleteShoppingCart(shoppingCart.getId());
            }else {
                shoppingCart.setNumber(shoppingCart.getNumber() -1 );
                shoppingCartMapper.updateNumberById(shoppingCart);
            }

        }

    }
}
